package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.pojo.VipCustomerLogPo;
import cc.linker.steplink.crm.merchanth5.pojo.VipPo;
import cc.linker.steplink.crm.merchanth5.service.VipCustomerLogService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 会员卡相关业务
 * @author huhj
 */
@RestController
@RequestMapping("/h5/vip")
public class H5VipCustomerController {

    @Autowired
    private VipCustomerLogService vipCustomerLogService;
    @Autowired
    private RequestFilter requestFilter;

    /**
     * 会员卡列表
     * @param request
     * @return
     */
    @GetMapping("/vipList")
    public List<VipPo> getViplist(HttpServletRequest request
    ) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        Map<String, Object> param = new HashMap<>();
        param.put("orgId", wxUser.getOrgId());
        param.put("customerId", wxUser.getCustomerId());
        return vipCustomerLogService.getViplist(param);
    }

    /**
     * 会员卡详情
     * @param id
     * @return
     */
    @GetMapping("/vipInfo")
    public VipPo vipInfo(Long id,HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        Map<String, Object> param = new HashMap<>();
        param.put("customerId", wxUser.getCustomerId());
        param.put("id", id);
        param.put("request",request);
        param.put("customerName",wxUser.getNickname());
        return vipCustomerLogService.vipInfo(param);
    }

    /**
     * 积分兑换-会员卡详情
     * @param goodsId
     * @return
     */
    @GetMapping("/integralVipInfo")
    public Map<String,Object> integralVipInfo(Long goodsId,HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        Map<String, Object> param = new HashMap<>();
        param.put("customerId", wxUser.getCustomerId());
        param.put("id", goodsId);
        param.put("orgId", wxUser.getOrgId());
        return vipCustomerLogService.getIntegralVipInfo(param);
    }

    /**
     * 开通会员
     *@param : vipId
     *@param : type
     *@param : optionId
     *@param : orderSource
     *
     */
    @PostMapping("/createVipOrder")
    public Object open(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser = requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String orgId = wxUser.getOrgId();
        String openId = wxUser.getOpenid();
        Integer payMode = wxUser.getPayMode();
        Long vipId = null;
        Integer type = null;
        if (!StringUtils.isEmpty(params.get("vipId"))) {
            vipId = Long.valueOf(params.get("vipId").toString());
        }
        if (!StringUtils.isEmpty(params.get("type"))) {
            type = Integer.valueOf(params.get("type").toString());
        }
        if (vipId == null || type == null) {
            throw GlobalException.build("该会员卡已失效！");
        }
        return vipCustomerLogService.open(params,customerId,vipId,orgId,payMode,openId,"");
    }

   /**
     * 兑换会员
     *@param : exchangeCode
     *
     */
    @PostMapping("/exchangeVip")
    public VipCustomerLogPo exchangeVip(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser = requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String orgId = wxUser.getOrgId();
        String phone = wxUser.getBindPhone();
        Integer payMode = wxUser.getPayMode();
        return vipCustomerLogService.exchangeVip(params,customerId,phone,orgId,payMode);
    }


    @PostMapping("/createIntegralVipOrder")
    public Object integralOpen(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        WxUser wxUser = requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String orgId = wxUser.getOrgId();
        String openId = wxUser.getOpenid();
        Integer payMode = wxUser.getPayMode();
        Long vipId = null;
        if (!StringUtils.isEmpty(params.get("vipId"))) {
            vipId = Long.valueOf(params.get("vipId").toString());
        }
        if (vipId == null) {
            throw GlobalException.build("该会员卡已失效！");
        }
        return vipCustomerLogService.integralOpen(params,customerId,vipId,orgId,payMode,openId);
    }

    @PostMapping("/getVipAppPaySign")
    public Object getVipAppPaySign(@RequestBody Map<String, Object> params){
        String orderSn = (String) params.get("orderSn");
        String openId = (String) params.get("openId");
        if (StringUtils.isEmpty(orderSn) || StringUtils.isEmpty(openId)) {
            throw GlobalException.build("参数错误");
        }
        return vipCustomerLogService.getVipAppPaySign(orderSn,openId);
    }

    @GetMapping("/getVipCoupon")
    public PageInfo getVipCoupon(HttpServletRequest request,
                                 @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                                 @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        WxUser wxUser = requestFilter.getWxUser(request);
        return vipCustomerLogService.getVipCoupon(wxUser.getCustomerId(), wxUser.getOrgId(), pageIndex, pageSize);
    }

}
